home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 711 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  91 lines

  1. Path: fire.wildfire.com!newsadm
  2. From: Stonewall Ballard <stoney@wildfire.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: beginner question - typecasting
  5. Date: Fri, 05 Jan 1996 18:04:44 -0500
  6. Organization: Wildfire Communications
  7. Message-ID: <30EDAE8C.582@wildfire.com>
  8. References: <4cei1r$s02@sun.cis.smu.edu> <30EBCED7.774@sto.fdata.se>
  9. NNTP-Posting-Host: zaphod.wildfire.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b4 (WinNT; I)
  14. CC: stoney@usa1.com
  15.  
  16. Niklas Mellin wrote:
  17. > Damon Bowman wrote:
  18. > >
  19. > > When you are typecasting, is there any difference between:
  20. > >
  21. > > a = int(x)
  22. > > and
  23. > > a = (int) x
  24. > [...]
  25. > Yes the first is not type casting, but a "type conversion".
  26. > When x is a  variables of built in types there is no
  27. > difference. But if x is a varible of some class for instance
  28. > the first form generates a call to that class' operator int(),
  29. > generating a compile time error message if there is no such
  30. > member function.
  31. > The 2nd form is an inheritance from plain C, and doesn't work
  32. > when x is of class type. It is also
  33. > not "safe", meaning that the compiler will not generate error
  34. > messages in certain situations, and you will get buggy code
  35. > or run time errors instead.
  36.  
  37. This is not true. The only difference between these forms is the syntax. 
  38. See section 3.2.5 in Stroustrup's "The C++ Programming Language" 2nd 
  39. edition.
  40.  
  41. Try compiling this:
  42.  
  43. ------
  44.  
  45. class y;
  46.  
  47. class x
  48. {
  49. public:
  50.   x(const y&);
  51. };
  52.  
  53. class y
  54. {
  55. };
  56.  
  57. int main()
  58. {
  59.   y aY;
  60.   x anX = (x)aY;   // cast notation using class name
  61. }
  62.  
  63. x::x(const y&)
  64. {
  65. }
  66.  
  67. ------
  68.  
  69. I tried this in GCC and Unixware 2.0's C++ compiler. Both made a call on 
  70. the conversion function.
  71.  
  72. > In general u should choose the conversion form if possible,
  73. > but sometimes a type cast can be handy. One case is if you
  74. > are using a 3rd part class library that is not well written
  75. > (read MFC) and they forgot to declare function parameters as
  76. > const and you have a const object you would like to pass to
  77. > that function. Then you can type cast your object or variable
  78. > to non const.
  79. > ---
  80. > Niklas Mellin---------------------------------------------------------------
  81. Stonewall Ballard
  82. stoney@wildfire.com (work)     Wildfire Communications, Inc.
  83. stoney@beeblebrox.com (home)   http://www.tiac.net/users/stoney/
  84.